package com.lzc.util;

import com.lzc.service.DataMigrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 数据迁移工具类
 * 提供简单的静态方法调用
 */
@Slf4j
@Component
public class DataMigrationUtil {

    private static DataMigrationService dataMigrationService;

    @Autowired
    public void setDataMigrationService(DataMigrationService dataMigrationService) {
        DataMigrationUtil.dataMigrationService = dataMigrationService;
    }

    /**
     * 开始数据迁移
     * @param batchSize 批处理大小
     * @param clearTarget 是否清理目标表
     * @return 迁移的记录数
     */
    public static long startMigration(int batchSize, boolean clearTarget) {
        if (dataMigrationService == null) {
            throw new RuntimeException("DataMigrationService 未初始化，请确保在Spring容器中调用");
        }

        log.info("=== 开始数据迁移 ===");
        log.info("批处理大小: {}, 清理目标表: {}", batchSize, clearTarget);

        try {
            // 显示源数据统计
            long sourceCount = dataMigrationService.getSourceDataCount();
            log.info("源表数据量: {} 条", sourceCount);

            if (sourceCount == 0) {
                log.warn("源表无数据，跳过迁移");
                return 0;
            }

            // 清理目标表（如果需要）
            if (clearTarget) {
                log.info("清理目标表...");
                dataMigrationService.clearTargetData();
            }

            // 开始迁移
            long startTime = System.currentTimeMillis();
            long migratedCount = dataMigrationService.migrateData(batchSize);
            long endTime = System.currentTimeMillis();

            long processingTime = endTime - startTime;
            double recordsPerSecond = (processingTime > 0) ? (double) migratedCount / (processingTime / 1000.0) : 0.0;

            String minutes = String.format("%.2f", processingTime / 60000.0);
            String rps = String.format("%.2f", recordsPerSecond);

            log.info("=== 数据迁移完成 ===");
            log.info("迁移记录数: {} 条", migratedCount);
            log.info("处理时间: {} ms ({} 分钟)", processingTime, minutes);
            log.info("处理速度: {} 条/秒", rps);

            // 估算6000万条数据的处理时间
            if (recordsPerSecond > 0) {
                double estimatedTime = 60000000.0 / recordsPerSecond / 3600;
                String hours = String.format("%.2f", estimatedTime);
                log.info("预计{}条数据需要: {} 小时", sourceCount,hours);
            }

            return migratedCount;

        } catch (Exception e) {
            log.error("数据迁移失败", e);
            throw new RuntimeException("数据迁移失败", e);
        }
    }

    /**
     * 开始数据迁移（使用默认参数）
     * @return 迁移的记录数
     */
    public static long startMigration() {
        return startMigration(2000, false);
    }

    /**
     * 获取源数据统计
     * @return 源表记录数
     */
    public static long getSourceDataCount() {
        if (dataMigrationService == null) {
            throw new RuntimeException("DataMigrationService 未初始化，请确保在Spring容器中调用");
        }
        return dataMigrationService.getSourceDataCount();
    }

    /**
     * 清理目标表
     */
    public static void clearTargetData() {
        if (dataMigrationService == null) {
            throw new RuntimeException("DataMigrationService 未初始化，请确保在Spring容器中调用");
        }
        log.info("清理目标表...");
        dataMigrationService.clearTargetData();
        log.info("目标表清理完成");
    }
}
